package com.huike.game.Activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.huike.game.R;
import com.huike.game.api.Api;
import com.huike.game.base.Game36;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

public class GameActivity36 extends AppCompatActivity {

    private ImageView iv_left_image;
    private TextView tv_left_text;
    private TextView tv_content_title;
    private ImageView iv_rigth_image;
    private TextView tv_right_text;
    private GridView gv_game36;
    private RequestQueue queue;
    private ProgressDialog show;
    private List<Game36.DataBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game36);
        queue = Volley.newRequestQueue(this);
        list = new ArrayList<>();
        initView();
        initData();


        gv_game36.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                AlertDialog.Builder builder = new AlertDialog.Builder(GameActivity36.this);
                AlertDialog dialog = builder.create();

                View inflate = View.inflate(GameActivity36.this, R.layout.game36_gv_video, null);
                VideoView vv_game36 = inflate.findViewById(R.id.vv_game36);


                vv_game36.setVideoPath("android.resource://" +getPackageName() + "/" +R.raw.ren);

                Log.e("---",getResources().toString());
                vv_game36.setMediaController(new MediaController(GameActivity36.this));

                vv_game36.start();


                dialog.setView(inflate);

                dialog.show();

            }
        });


    }

    private void show() {
        show = ProgressDialog.show(this, "提示", "数据加载中...");
        show.show();
    }

    private void hide() {
        show.dismiss();
    }

    private void initData() {
        show();
        JsonObjectRequest request = new JsonObjectRequest(1, Api.api + "personCenterinfo", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                hide();

                try {
                    JSONArray data = jsonObject.getJSONArray("data");
                    Game36.DataBean bean;
                    for (int i = 0; i < data.length(); i++) {
                        bean= new Game36.DataBean();
                        JSONObject object = data.getJSONObject(i);
                        String image = object.getString("image");
                        bean.setImage(image);
                        String carName = object.getString("carName");
                        bean.setCarName(carName);
                        list.add(bean);
                    }

                    gv_game36.setAdapter(new GViderHelper());


                } catch (JSONException e) {
                    e.printStackTrace();
                }


            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                hide();
                Toast.makeText(GameActivity36.this, "数据加载异常!", Toast.LENGTH_SHORT).show();
            }
        });

        queue.add(request);


    }

    private void initView() {
        iv_left_image = (ImageView) findViewById(R.id.iv_left_image);
        tv_left_text = (TextView) findViewById(R.id.tv_left_text);
        tv_content_title = (TextView) findViewById(R.id.tv_content_title);
        iv_rigth_image = (ImageView) findViewById(R.id.iv_rigth_image);
        tv_right_text = (TextView) findViewById(R.id.tv_right_text);
        gv_game36 = findViewById(R.id.gv_game36);

        iv_rigth_image.setVisibility(View.GONE);
        tv_right_text.setVisibility(View.GONE);

        tv_content_title.setText("车辆违章");

        iv_left_image.setImageResource(R.drawable.tiao);
        tv_left_text.setVisibility(View.GONE);


    }

    class GViderHelper extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {

            ViewHolder holder = null;
            if (view == null) {
                view = View.inflate(GameActivity36.this, R.layout.game36_gv, null);
                holder = new ViewHolder(view);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }


            Game36.DataBean bean = list.get(i);

            final ViewHolder finalHolder = holder;
            ImageRequest request = new ImageRequest(bean.getImage(), new Response.Listener<Bitmap>() {
                @Override
                public void onResponse(Bitmap bitmap) {

                    finalHolder.iv_game36_image.setImageBitmap(bitmap);

                }
            }, 0, 0, ImageView.ScaleType.CENTER, Bitmap.Config.ARGB_8888, null);

            queue.add(request);

            holder.tv_game36_name.setText(bean.getCarName());


            return view;
        }


    }

    class ViewHolder {
        public View rootView;
        public ImageView iv_game36_image;
        public TextView tv_game36_name;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.iv_game36_image = (ImageView) rootView.findViewById(R.id.iv_game36_image);
            this.tv_game36_name = (TextView) rootView.findViewById(R.id.tv_game36_name);
        }

    }

}
